﻿using System;
using System.Globalization;
using System.Text;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.HtmlHelpers
{
    public static class PagingHelpers
    {
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string>pageUrl)
        {
            var result = new StringBuilder(); //đoạn string để inner vào html
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                var tag = new TagBuilder("a"); // add 1 tag vào đầu
                tag.MergeAttribute("href", pageUrl(i));
                // add href (pageurl(i) sẽ được truyền vào lại trong ham Pagelinks của List.cshtml
                // Url.Action("List", x => {new page = x}-> i dot sau se dc gán vào hàm (khởi tạo new lại)
                tag.InnerHtml = i.ToString(CultureInfo.InvariantCulture);//inner vào html số trang (1, 2)
                if (i==pagingInfo.CurrentPage)
                {
                    tag.AddCssClass("selected");
                }
                result.Append(tag.ToString()); //add tat ca các tag vào
            }
            return MvcHtmlString.Create(result.ToString()); // su dung mvchtmlstring de dua vào html
        }

    }
}